package com.jt.interceptor;

import com.jt.pojo.User;
import com.jt.util.CookieUtil;
import com.jt.util.ObjectMapperUtil;
import com.jt.util.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class UserInterceptor implements HandlerInterceptor {

    private static final String JT_TICKET="JT_TICKET";

    @Autowired
    private JedisCluster jedisCluster;

    /**
     * 返回值说明：
     *      1.false  表示拦截,一般配合重定向的方式使用
     *      2.true   表示放行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws IOException
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws IOException {
        //String ticket=null;
        //1.判断cookie中是否有记录
        Cookie cookie = CookieUtil.getCookie(request, JT_TICKET);
//        Cookie[] cookies = request.getCookies();
//        if(cookies!=null&&cookies.length>0){
//            for (Cookie cookie : cookies) {
//                if("JT_TICKET".equals(cookie.getName())){
//                    ticket=cookie.getValue();
//                    break;
//                }
//            }
//        }
        //2.判断cookie数据是否有效
        if(cookie!=null){
            String ticket=cookie.getValue();
            if(StringUtils.hasLength(ticket)){
                //校验redis中是否有结果
                if(jedisCluster.exists(ticket)){
                    String json = jedisCluster.get(ticket);
                    User user = ObjectMapperUtil.toObject(json, User.class);
                    //利用request对象进行数据传递
                    request.setAttribute("JT_USER", user);

                    //threadlocal机制
                    UserThreadLocal.setUser(user);
                    //存在数据，表示用户登录过
                    return true;
                }
            }
            //如果cookie没值，则删掉cookie
            CookieUtil.addCookie(response, JT_TICKET,"","/","jt.com",0 );

        }
//        if(!StringUtils.hasLength(ticket)){
//            if(jedisCluster.exists(ticket)){
//                String userJSON = jedisCluster.get(ticket);
//                User user = ObjectMapperUtil.toObject(userJSON, User.class);
//                //利用request对象进行数据传递
//                request.setAttribute("JT_USER", user);
//                return true;//表示用户已经登录
//            }
//        }

        //3.如果数据为空,重定向到用户登录页面
        response.sendRedirect("/user/login.html");
        return false;//表示拦截
    }

    //为了防止内存泄漏，将多余的数据删除

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        request.removeAttribute("JT_USER");
        UserThreadLocal.remove();
    }
}
